package com.xiaoge.demo.dao;

import com.xiaoge.demo.dto.AddressInfo;
import com.xiaoge.demo.pojo.Address;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface AddressDao extends JpaRepository<Address, Long> {

    /**
     * 通过dto构造返回数据
     *
     * @param userId
     * @return
     */
    @Query("select new com.xiaoge.demo.dto.AddressInfo(a.address,a.door,a.uid) from Address a where a.is_delete = 0 and a.uid = :userId")
    List<AddressInfo> getUserAddress(@Param("userId") String userId);

    /**
     * 通过原生sql进行查询
     *
     * @param userId
     * @return
     */
    @Query(value = "select a.id as uid,a.name,a.token,b.id as aid,b.address,b.door from user a left join address b on a.id = b.uid where a.is_delete = 0 and a.id = :userId order by b.id desc", nativeQuery = true)
    List<Map> getAddressAndUserInfo(@Param("userId") String userId);
}
